/*
 * @Author: silence silence9685@163.com
 * @Date: 2023-07-26 17:51:55
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2023-10-18 15:43:39
 * @FilePath: \ems_neutral_app\lib\pages\PowerStation\Controller\PowerStationDetailController.dart
 * @Description: 电站详细页控制器
 */
import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
import 'package:sil_utils_package/tools/SilTools.dart';

import '../../../api/ApiUtils.dart';
import '../../../api/request/PowerStationRequest.dart';
import '../../../bloc/userbloc/user_bloc.dart';
import '../../../entities/PowerStationEntity.dart';

class PowerStationDetailController extends GetxController {
  /// 当前电站的信息
  Rx<PowerStationEntity> powerStationEntity = PowerStationEntity().obs;

  /// 获取基础信息发电量、收益
  powerStationBaseDetail({
    required BuildContext context,
    PowerStationEntity? sourceEntity,
    Function(dynamic)? successHandler,
  }) {
    String? powerRecordId = UserBlocTool.userEntity?.powerRecordId;
    if (SilTools.stringIsEmpty(powerRecordId)) {
      print('数采器id不能为空');
      return;
    }

    apiShow(context, api: api_power_station_base_info(deviceId: powerRecordId!),
        success: (res) {
      /// 合并电站信息
      PowerStationEntity entity = res.data;
      PowerStationEntity powerStationEntity =
          sourceEntity ?? this.powerStationEntity.value;
      powerStationEntity.stored_total = entity.stored_total;
      powerStationEntity.use_electric_total = entity.use_electric_total;
      powerStationEntity.diesel_oil = entity.diesel_oil;
      powerStationEntity.forest = entity.forest;
      powerStationEntity.generation_total = entity.generation_total;
      powerStationEntity.create_time = entity.create_time;
      powerStationEntity.co2 = entity.co2;
      powerStationEntity.coal = entity.coal;
      powerStationEntity.moneyUnit = entity.moneyUnit;
      powerStationEntity.money_total = entity.money_total;
      powerStationEntity.equipmentSelection = entity.equipmentSelection;
      powerStationEntity.batteryVoltage = entity.batteryVoltage;
      powerStationEntity.outputCurrent = entity.outputCurrent;
      // powerStationEntity.real_power = entity.real_power;
      powerStationEntity.pv_one = entity.pv_one;
      powerStationEntity.pv_two = entity.pv_two;
      entity = powerStationEntity;

      /// 更新电站信息，此处更新使用'vulue='无效，需要使用update
      this.powerStationEntity.update((val) {
        val = entity;
      });
      this.powerStationEntity.value = entity;
      if (successHandler != null) successHandler(res);
    }, showLoading: false);
  }
}
